package com.ysm.smfu.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.ysm.smfu.R;
import com.ysm.smfu.area.Area;
import com.ysm.smfu.area.FileUtil;
import com.ysm.smfu.contans.SpKey;
import com.ysm.smfu.fragment.BendiFragment;
import com.ysm.smfu.fragment.OnLineFragment;
import com.ysm.smfu.utils.ThreadUtils;
import com.ysm.smfu.utils.UIUtils;
import com.ysm.smfu.welcome.SharedConfig;
import com.zhy.autolayout.AutoFrameLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/9/27.
 */
public class FoundActivity extends BaseActivity {

    @BindView(R.id.title_body)
    TextView titleBody;
    @BindView(R.id.mainActivity_topbar_img)
    ImageView mainActivityTopbarImg;
    @BindView(R.id.activity_main_top_bar)
    AutoRelativeLayout activityMainTopBar;
    @BindView(R.id.fragment_found_container)
    AutoFrameLayout fragmentFoundContainer;
    @BindView(R.id.close)
    TextView close;
    private int found_position;
    private Fragment fragment;
    private OnBack onBack;
    private Area area;
    private String cityId = "";

    @Override
    protected void init() {
        super.init();
        found_position = getIntent().getIntExtra("found", -1);

        getCityId();
    }

    private void getCityId() {
        final SharedConfig sharedConfig = new SharedConfig(UIUtils.getContext());
        final String bdLoc = sharedConfig.getCommoneStringInfo(SpKey.BDLOC);

        ThreadUtils.runOnBackThread(new Runnable() {
            @Override
            public void run() {
                String area_str = "";
                String str = sharedConfig.getCommoneStringInfo(SpKey.AAASSEST);
                if (!TextUtils.isEmpty(str))
                    area_str = str;
                else {
                    area_str = FileUtil.readAssets(UIUtils.getContext(), "aa.json");
                    sharedConfig.saveCommoneStringInfo(SpKey.AAASSEST, area_str);
                }
                area = new Gson().fromJson(area_str, Area.class);
                if (!TextUtils.isEmpty(bdLoc)) {
                    cityId = area.getIdByName(bdLoc);
//                保存定位的城市id到本地
                    sharedConfig.saveCommoneStringInfo(SpKey.LOCID, cityId);
                }
            }
        });
    }

    @Override
    protected void initViews() {
        super.initViews();
        setContentView(R.layout.activity_found);
        ButterKnife.bind(this);
        close.setVisibility(View.VISIBLE);
        mainActivityTopbarImg.setVisibility(View.GONE);
//        线上商城Fragment
        fragment = new OnLineFragment();
        Bundle bundle = new Bundle();
        switch (found_position) {
            case 0:
                activityMainTopBar.setVisibility(View.GONE);
                titleBody.setVisibility(View.GONE);
                fragment = new BendiFragment();
                break;
            case 1:
                titleBody.setText("精品商城");
                bundle.putString(SpKey.URL, SpKey.ONLINEURL);
                fragment.setArguments(bundle);
                break;
            case 2:
                titleBody.setText("升级VIP");
                bundle.putString(SpKey.URL, SpKey.VIPURL);
                fragment.setArguments(bundle);
                break;
            case 10:
                titleBody.setText("充值");
                bundle.putString(SpKey.URL, SpKey.CHONGZHI);
                fragment.setArguments(bundle);
                break;
            case 3:
                titleBody.setText("兑换券商城");
                bundle.putString(SpKey.URL, SpKey.GWQURL);
                fragment.setArguments(bundle);
                break;
            case 4:
                titleBody.setText("邀请好友");
                String invitUrl = getIntent().getStringExtra(SpKey.INVITURL);
                bundle.putBoolean(SpKey.INVITURL, true);
                bundle.putString(SpKey.URL, invitUrl);
                fragment.setArguments(bundle);
                break;
            case 5:
                titleBody.setText("订单信息");
                bundle.putBoolean(SpKey.DINGDANRUL, true);
                bundle.putString(SpKey.URL, SpKey.DINGDAN_URL);
                fragment.setArguments(bundle);
                break;
            case 6:
                titleBody.setText("我的好友");
                bundle.putString(SpKey.URL, SpKey.MY_XIAJI);
                bundle.putBoolean(SpKey.MYXIAJIURL, true);
                fragment.setArguments(bundle);
                break;
            case 7:
                titleBody.setText("待付款");
                bundle.putBoolean(SpKey.DAiFuKuan, true);
                bundle.putString(SpKey.STATE, "00");
                bundle.putString(SpKey.URL, SpKey.DINGDAN_URL);
                fragment.setArguments(bundle);
                break;
            case 8:
                titleBody.setText("待发货");
                bundle.putBoolean(SpKey.DAiFuKuan, true);
                bundle.putString(SpKey.STATE, "01");
                bundle.putString(SpKey.URL, SpKey.DINGDAN_URL);
                fragment.setArguments(bundle);
                break;
            case 9:
                titleBody.setText("待收货");
                bundle.putBoolean(SpKey.DAiFuKuan, true);
                bundle.putString(SpKey.STATE, "02");
                bundle.putString(SpKey.URL, SpKey.DINGDAN_URL);
                fragment.setArguments(bundle);
                break;
            case 11:
                titleBody.setText("地址信息");
                bundle.putString(SpKey.URL, SpKey.ADDRESSURL);
                fragment.setArguments(bundle);
                break;
            case 12:
                titleBody.setText("支付结果");
                String code = getIntent().getStringExtra("code");
                bundle.putString(SpKey.URL, SpKey.WXURL);
                bundle.putBoolean("isWX", true);
                bundle.putString("code", code);
                fragment.setArguments(bundle);
                break;
            case 13:
//                广告信息
                String realLink = getIntent().getStringExtra("realLink");
                String realId = getIntent().getStringExtra("realId");
                bundle.putString(SpKey.ADLINK, realLink);
                bundle.putString(SpKey.ADID, realId);
                bundle.putBoolean(SpKey.ISAD, true);
                fragment.setArguments(bundle);
                break;
            case 14:
                titleBody.setText("钱包");
                bundle.putString(SpKey.URL, SpKey.WALLALTURL);
                fragment.setArguments(bundle);
                break;
            case 15:
                titleBody.setText("支付成功");
                String code2 = getIntent().getStringExtra("code");
                bundle.putString(SpKey.URL, SpKey.WXACTIVITYURL);
                bundle.putBoolean("isWX", true);
                bundle.putString("code", code2);
                fragment.setArguments(bundle);
                break;
            case 16://产品详情
                String goodsId = getIntent().getStringExtra("goodsId");
                bundle.putString("goodsId", goodsId);
                fragment.setArguments(bundle);
                break;
        }
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_found_container, fragment).commit();
    }

    @OnClick({R.id.back, R.id.close})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                if (onBack != null) {
                    onBack.onBack();
                } else {
                    sendBroadcastForUpdateInfo();
                    finish();
                }
                break;
            case R.id.close:
                sendBroadcastForUpdateInfo();
                finish();
                break;
        }
    }

    /**
     * 发送网络更新个人信息
     */
    private void sendBroadcastForUpdateInfo() {
        Intent intent = new Intent();
        intent.setAction("updateUserInfo_online");
        sendBroadcast(intent);
    }

    /**
     * 无法调用支付宝客户端进行网页支付时，隐藏标题
     *
     * @param b
     */
    public void setTitleGone(boolean b) {
        if (b)
            activityMainTopBar.setVisibility(View.VISIBLE);
        else
            activityMainTopBar.setVisibility(View.GONE);
    }

    public interface OnBack {
         void onBack();
    }

    public void setOnBack(OnBack onBack) {
        this.onBack = onBack;
    }

    @Override
    public void onBackPressed() {
        if (onBack != null) {
            onBack.onBack();
        } else {
            finish();
        }
    }

    /**
     * 通过onLineFragment获取H5标题并设置
     *
     * @param title
     */
    public void setTitle(String title) {
        if (!TextUtils.isEmpty(title))
            titleBody.setText(title);
        else
            titleBody.setText("数贸服务");
    }
}
